package us.proyecto.canarios.actions.concursos;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jxl.Workbook;
import jxl.format.CellFormat;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableCellFeatures;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.log.Log;

@Name("generacionPlantilla")
@Scope(ScopeType.SESSION)
public class GeneraPlantillas {

    @Logger
    Log log;
    
    
    
    public void generaIndividual(int id){
            HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);

            URL uri = this.getClass().getClassLoader().getResource("plantillas/Plantilla individual.xls");

            String name = "\"plantillaIndividual.xls\"";

            File file;
            File temp = (File) session.getServletContext().getAttribute("javax.servlet.context.tempdir");

            String path = new String(temp.getAbsolutePath() + "/temp.xls");
            File tempFile = new File(path);

            try {
                file = new File(uri.toURI());

                try {

                    Workbook template = Workbook.getWorkbook(file);
                    WritableWorkbook wb = Workbook.createWorkbook(tempFile, template);
                   // Date fechaAux=new Date(fechaInicio.getTime());
                    WritableSheet sheet=wb.getSheet(0);
                    
                    WritableCell celda = sheet.getWritableCell("E1");
                    celda = new Label(4,0,""+id);
                    sheet.addCell(celda);
                   
                    wb.write();
                    wb.close();
                    
                    descargarExcel(tempFile, name);
                    }catch (NumberFormatException e) {
                       FacesMessages.instance().add("Debe introducir un número correcto");
                    }
                }catch (Exception e) {
                    e.printStackTrace();
                }
            
            
        
    }
    
    
    public void generaGrupo(int id){
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);

        URL uri = this.getClass().getClassLoader().getResource("plantillas/Plantilla grupo.xls");

        String name = "\"plantillaGrupo.xls\"";

        File file;
        File temp = (File) session.getServletContext().getAttribute("javax.servlet.context.tempdir");

        String path = new String(temp.getAbsolutePath() + "/temp.xls");
        File tempFile = new File(path);

        try {
            file = new File(uri.toURI());

            try {

                Workbook template = Workbook.getWorkbook(file);
                WritableWorkbook wb = Workbook.createWorkbook(tempFile, template);
               // Date fechaAux=new Date(fechaInicio.getTime());
                WritableSheet sheet=wb.getSheet(0);
                
                WritableCell celda = sheet.getWritableCell("H1");
                celda = new Label(7,0,""+id);
                sheet.addCell(celda);
               
                wb.write();
                wb.close();
                
                descargarExcel(tempFile, name);
                }catch (NumberFormatException e) {
                   FacesMessages.instance().add("Debe introducir un número de días válido");
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        
        
    
}
   
    private void descargarExcel(File file, String name) {

        int l = (int) file.length();
        byte[] b = new byte[l];

        FileInputStream f;

        try {
            f = new FileInputStream(file);

            f.read(b);
            f.close();
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        FacesContext faces = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) faces.getExternalContext().getResponse();

        response.setContentType("application/vnd.ms-excel");
        response.setContentLength(b.length);
        response.setHeader("Content-disposition", "attachment; filename=" + name);

        try {
            ServletOutputStream desc = response.getOutputStream();
            desc.write(b);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        faces.responseComplete();

    }
    
}
